package com.sczfdf.www.sda.gov.cn.modo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.model.AfterExtractor;
import us.codecraft.webmagic.model.HttpRequestBody;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.model.annotation.ExtractBy.Type;
import us.codecraft.webmagic.model.annotation.TargetUrl;
import us.codecraft.webmagic.utils.HttpConstant;

@TargetUrl("[0-9A-Za-z\\.://\\?]+=getAttachmentCpbz")
public class GovImageDO  implements AfterExtractor{
	@Override
	public void afterProcess(Page page) {
		this.ssid = this.ssid.replaceAll("\n", "");
		for (int i = 0; i < this.imageId.size(); i++) {
			all.append(this.imageName.get(i) + " : " + 
					"125.35.6.80:8080/ftba/itownet/download.do?method=" + 
					"downloadFile&fid=" + this.imageId.get(i) + 
					"&ssid=" + this.ssid + "; ");
		}
	}

	public static Request createRequest(String processid) {
		Request request = new Request("http://125.35.6.80:8080/ftba/itownet/fwAction.do?method=getAttachmentCpbz");

		request.setMethod(HttpConstant.Method.POST);
		request.addHeader("Accept", "*/*");
		request.addHeader("Accept-Encoding", "gzip, deflate");
		request.addHeader("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
		request.addHeader("Cache-Control", "no-cache");
		request.addHeader("Connection", "keep-alive");
		request.addHeader("Content-Type", "application/x-www-form-urlencoded;utf-8");
		request.addHeader("Host", "125.35.6.80:8080");
		request.addHeader("Pragma", "no-cache");
		request.addHeader("Referer", "http://125.35.6.80:8080/ftba/fw.jsp");
//		request.addHeader("User-Agent", UserAgentTool.getUserAgent());
		request.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko");
		request.addHeader("X-Requested-With", "XMLHttpRequest");

		Map<String, Object> params = new HashMap<>();
		params.put("processId", processid);
		request.setRequestBody(HttpRequestBody.form(params,"utf-8"));

		return request;
	}

	@ExtractBy(value="$..processid", type=Type.JsonPath)
	private String KEY;
	@ExtractBy(value="$..fileName", type=Type.JsonPath)
	private List<String> imageName = new ArrayList<String>();
	@ExtractBy(value="$..id", type=Type.JsonPath)
	private List<String> imageId = new ArrayList<String>();
	@ExtractBy(value="$.ssid", type=Type.JsonPath)
	private String ssid;

	private StringBuffer all = new StringBuffer();

	public String getKEY() {
		return KEY;
	}
	public void setKEY(String kEY) {
		KEY = kEY;
	}
	public List<String> getImageName() {
		return imageName;
	}
	public void setImageName(List<String> imageName) {
		this.imageName = imageName;
	}
	public List<String> getImageId() {
		return imageId;
	}
	public void setImageId(List<String> imageId) {
		this.imageId = imageId;
	}
	public String getSsid() {
		return ssid;
	}
	public void setSsid(String ssid) {
		this.ssid = ssid;
	}
	public String getAll() {
		return all.toString();
	}
	public void setAll(StringBuffer all) {
		this.all = all;
	}
	@Override
	public String toString() {
		return "GovImageDO [KEY=" + KEY + ", imageName=" + imageName + ", imageId=" + imageId + ", ssid=" + ssid
				+ ", all=" + all + "]";
	}
}
